<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="shop-info-box">
					<view class="info-item flexWrapNo">
						<view class="item-title">基础折扣:</view>
						<input placeholder="请输入折扣" placeholder-class="placeholderclass" type="digit"
							v-model="discount" />
					</view>
					<view class="remaks-box">
						用户在分段折扣之外的时间使用通卡支付所享受的折扣，如输入68，则为订单原价*68%。 通卡支付不可使用优惠券、美团、抖音团购券。
					</view>
					<view class="time-item" v-for="(item,index) in timeArr" :key='index'>
						<image src="../../static/user/close.png" class="close-btn" @tap="delTime(index)"></image>
						<view class="info-item flexWrapNo">
							<view class="item-title">分段时间{{Number(index+1)}}:</view>
							<view class="flexWrap">
								<picker mode="time" :value="item.starTime" start="00:00" end="24:00" :data-index='0'
									:data-arrIndex="index" @change="bindTimeChange">
									<view class="select-input" v-if="item.starTime">{{item.starTime}}</view>
									<view class="select-input" v-else>请选择时间</view>
								</picker>
								<text class="small">至</text>
								<picker mode="time" :value="item.endTime" start="00:00" end="24:00" :data-index='1'
									:data-arrIndex="index" @change="bindTimeChange">
									<view class="select-input" v-if="item.endTime">{{item.endTime}}</view>
									<view class="select-input" v-else>请选择时间</view>
								</picker>
							</view>
						</view>
						<view class="info-item flexWrapNo">
							<view class="item-title">分段折扣{{Number(index+1)}}:</view>
							<input placeholder="请输入折扣" placeholder-class="placeholderclass" v-model="item.discount" type="digit"/>
						</view>
					</view>
					<view class="remaks-box">
						用户在该段时间可享受的特殊折扣，超出该时段为基础折扣。可添加多个时段，但多个时段不允许时间有重叠。
					</view>
				</view>
				<view class="determine-btn" @tap="addTime">+添加分段时间</view>
				<view class="bottom-box flexWrapNo">
					<view class="define-btn" @tap="editPass()">确定</view>
				</view>
				
				<!--操作指引悬浮按钮 -->
				<guideline :path="'pages/manage/passCard'"></guideline>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageOpc: 0,
				discount: "",
				timeArr: [{
					starTime: "",
					endTime: "",
					discount: ""
				}]
			}
		},
		onLoad(ops) {
			this.getPassInfo();
		},
		onReady() {
			setTimeout(() => {
				this.pageOpc = 1
			}, 100)
		},
		methods: {
			// 选择时间
			bindTimeChange(e) {
				let index = e.target.dataset.index
				let arrIndex = e.target.dataset.arrindex
				if (index == 0) {
					this.timeArr[arrIndex].starTime = e.target.value
				} else {
					this.timeArr[arrIndex].endTime = e.target.value
				}
			},
			addTime() {
				let item = {
					starTime: "",
					endTime: "",
					discount: ""
				}
				this.timeArr.push(item)
			},
			delTime(index) {
				// if(this.timeArr.length==1){
				// 	return;
				// }
				this.timeArr.splice(index, 1)
			},
			// 获取通卡优惠信息
			getPassInfo() {
				this.$request.get(this.$api.getPassInfo, {
					store_id: uni.getStorageSync('store_id')
				}, {
					token: true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code == 1) {
						this.discount = data.discount
						this.timeArr = data.discount_json || []
					}
				})
			},
			editPass() {
				if (!this.discount) {
					this.$tools.showToast('请设置基础折扣')
					return;
				}
				if (this.discount > 100 || this.discount == 0) {
					this.$tools.showToast('请设置正确的折扣')
					return;
				}
				let arrEmpty = false //是否填写完整
				let arrDis = false
				let isOverlap=this.isOverlap(this.timeArr);
				if (this.timeArr.length >= 1) {
					arrEmpty = this.timeArr.some(item => item.starTime === "" || item.endTime === "" || item.discount ==="")
				}
				if (this.timeArr.length >= 1) {
					arrDis = this.timeArr.some(item => item.discount === "0" || item.discount > 100)
				}
				if (arrEmpty) {
					this.$tools.showToast('请设置完整后提交')
					return;
				}
				if (arrDis) {
					this.$tools.showToast('折扣区间只能设置在0~100')
					return;
				}
				if (isOverlap) {
					this.$tools.showToast('折扣时间不能重合')
					return;
				}
				this.$request.post(this.$api.passditEdit, {
					store_id: uni.getStorageSync('store_id'),
					discount: this.discount,
					discount_json: JSON.stringify(this.timeArr)
				}, {
					token: true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code == 1) {
						this.$tools.showToast('设置成功')
					}
				})
			},
			isOverlap(timeSlots) {
				timeSlots.sort((a, b) => {
					return new Date(`1970/01/01 ${a.starTime}`) - new Date(`1970/01/01 ${b.starTime}`);
				});
				for (let i = 1; i < timeSlots.length; i++) {
					const prevEnd = new Date(`1970/01/01 ${timeSlots[i - 1].endTime}`);
					const curStart = new Date(`1970/01/01 ${timeSlots[i].starTime}`);
					if (curStart <= prevEnd) {
						return true;
					}
				}
				return false;
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 0 36upx;
		padding-bottom: 120upx;

		.shop-info-box {
			.info-item {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 40upx;
				color: #FFFFFF;
				padding: 30upx 0;
				border-bottom: 1upx solid #36355E;

				picker {
					flex: none;
				}

				.item-title {
					margin-right: 32upx;
					flex: none;
				}

				.small {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					margin: 0 40upx;
				}

				.select-input {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
					flex: 1;
				}

				input {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
					flex: 1;
				}
				.icon-more {
					border-color: #FFFFFF;
				}

				.placeholderclass {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
				}
			}

			.remaks-box {
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #82819B;
				line-height: 40rpx;
				padding-left: 158upx;
				margin-top: 12upx;
			}
		}

		.determine-btn {
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			width: 322rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #26BCFD;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 78upx auto;
		}

		.bottom-box {
			height: 120rpx;
			background: #1D1C5C;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			margin: auto;
			justify-content: center;
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;

			.define-btn {
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 80rpx;
				width: 322rpx;
				height: 80rpx;
				background: #26BCFD;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				text-align: center;
			}
		}

		.time-item {
			padding-top: 40upx;
			position: relative;

			.close-btn {
				width: 40upx;
				height: 40upx;
				position: absolute;
				top: 20upx;
				right: 0;
			}
		}
	}
</style>